package com.cysyz.mylib.jbpm.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.task.Task;

import com.cysyz.mylib.jbpm.IJBPMCommonService;
import com.cysyz.mylib.jbpm.IJBPMProcessEnginSimpleOperator;
import com.cysyz.mylib.jbpm.impl.JBPMProcessEngineSimpleOperateImpl.ProcessDefinitionCondsType;
import com.cysyz.mylib.jbpm.impl.JBPMProcessEngineSimpleOperateImpl.ProcessDefinitionQueryOrderType;
import com.cysyz.mylib.jbpm.impl.JBPMProcessEngineSimpleOperateImpl.ProcessDefintionProperty;
import com.cysyz.mylib.jbpm.impl.JBPMProcessEngineSimpleOperateImpl.ProcessDefintionQueryConds;

public class JBPMCommonServiceImpl implements IJBPMCommonService {
	private IJBPMProcessEnginSimpleOperator simpleOperator;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cysyz.mylib.jbpm.impl.IJBPMCommonService#setSimpleOperator(com.cysyz
	 * .mylib.jbpm.IJBPMProcessEnginSimpleOperator)
	 */
	public void setSimpleOperator(IJBPMProcessEnginSimpleOperator simpleOperator) {
		this.simpleOperator = simpleOperator;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cysyz.mylib.jbpm.impl.IJBPMCommonService#getSimpleOperator()
	 */
	public IJBPMProcessEnginSimpleOperator getSimpleOperator() {
		return this.simpleOperator;
	}

	public JBPMCommonServiceImpl() {

	}

	public JBPMCommonServiceImpl(IJBPMProcessEnginSimpleOperator simpleOperator) {
		this.simpleOperator = simpleOperator;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cysyz.mylib.jbpm.impl.IJBPMCommonService#
	 * findAllLastVersionProcessDefinition()
	 */
	public List<ProcessDefinition> findAllLastVersionProcessDefinition() {
		/** 按照流程定义的版本升序排列，最新版本的在最后 */
		ProcessDefintionQueryConds conds = new ProcessDefintionQueryConds();
		conds.addOrder(ProcessDefinitionQueryOrderType.ORDERASC,
				ProcessDefintionProperty.PROPERTY_VERSION);
		List<ProcessDefinition> list = simpleOperator
				.findProcessDefinitionList(conds);

		/**
		 * Map:当key值相同的情况下，后一次版本的值将替换前一次版本的值 * map集合的key：流程定义的key *
		 * map集合的value：流程定义对象
		 * */
		Map<String, ProcessDefinition> map = new LinkedHashMap<String, ProcessDefinition>();
		if (list != null && list.size() > 0) {
			for (ProcessDefinition pd : list) {
				map.put(pd.getKey(), pd);
			}
		}
		/** pdList中存放所有最新版本的流程定义 */
		return new ArrayList<ProcessDefinition>(map.values());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cysyz.mylib.jbpm.impl.IJBPMCommonService#
	 * findLastVersionProcessDefinitionByKey(java.lang.String)
	 */
	public ProcessDefinition findLastVersionProcessDefinitionByKey(String key) {
		if (key == null || key.isEmpty() || key.trim().isEmpty()) {
			return null;
		}
		/** 按照流程定义的版本升序排列，最新版本的在最后 */
		ProcessDefintionQueryConds conds = new ProcessDefintionQueryConds();
		conds.addCond(ProcessDefinitionCondsType.KEY, key);
		conds.addOrder(ProcessDefinitionQueryOrderType.ORDERASC,
				ProcessDefintionProperty.PROPERTY_VERSION);
		List<ProcessDefinition> list = simpleOperator
				.findProcessDefinitionList(conds);

		/**
		 * Map:当key值相同的情况下，后一次版本的值将替换前一次版本的值 * map集合的key：流程定义的key *
		 * map集合的value：流程定义对象
		 * */
		Map<String, ProcessDefinition> map = new LinkedHashMap<String, ProcessDefinition>();
		if (list != null && list.size() > 0) {
			for (ProcessDefinition pd : list) {
				map.put(pd.getKey(), pd);
			}
		}
		/** pdList中存放所有最新版本的流程定义 */
		return new ArrayList<ProcessDefinition>(map.values()).get(0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cysyz.mylib.jbpm.impl.IJBPMCommonService#deleteProcessDefinitionByKey
	 * (java.lang.String)
	 */
	public void deleteProcessDefinitionByKey(String key) {
		if (key == null || key.isEmpty() || key.trim().isEmpty()) {
			throw new IllegalArgumentException("key值为空");
		}
		/** 使用流程定义的key，先查询对应该key值下所有版本的流程定义列表 */
		ProcessDefintionQueryConds conds = new ProcessDefintionQueryConds();
		conds.addCond(ProcessDefinitionCondsType.KEY, key);
		List<ProcessDefinition> list = simpleOperator
				.findProcessDefinitionList(conds);
		if (list != null && list.size() > 0) {
			for (ProcessDefinition pd : list) {
				// 获取每个部署ID的对象
				String deploymentId = pd.getDeploymentId();
				simpleOperator.deleteProcessDefinition(deploymentId);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cysyz.mylib.jbpm.impl.IJBPMCommonService#
	 * deployNewProcessDefinitionByZipFile(java.io.File)
	 */
	public void deployNewProcessDefinitionByZipFile(File zipFile)
			throws FileNotFoundException {
		simpleOperator.deploymentProcessDefinitionByClassPathZip(zipFile);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cysyz.mylib.jbpm.impl.IJBPMCommonService#pintProcessDefinitionInfo
	 * (java.util.List)
	 */
	public void pintProcessDefinitionInfo(List<ProcessDefinition> pdList) {
		for (ProcessDefinition pd : pdList) {
			System.out.println("流程定义ID：" + pd.getId());// 区分流程定义的惟一值：由流程定义key+流程定义的版本号组成
			System.out.println("流程定义的名称：" + pd.getName());// 资源文件helloworld.jpdl.xml中name="helloworld"其中name的属性值
			System.out.println("流程定义的key：" + pd.getKey());// 资源文件helloworld.jpdl.xml中key="helloworld"其中key的属性值，如果key值不写，默认name的属性值
			System.out.println("流程定义的版本号：" + pd.getVersion());// 在key值相同的情况下，版本号要逐次+1，初始值是1
			System.out.println("资源图片名称：" + pd.getImageResourceName());// 图片的名称
			System.out.println("部署对象ID：" + pd.getDeploymentId());
			System.out.println("---------------------------------------");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cysyz.mylib.jbpm.impl.IJBPMCommonService#printTaskInfo(java.util.
	 * List)
	 */
	public void printTaskInfo(List<Task> list) {
		for (Task task : list) {
			System.out.println("任务ID：" + task.getId());
			System.out.println("任务名称：" + task.getName());
			System.out.println("任务的办理人：" + task.getAssignee());
			System.out.println("任务的创建时间：" + task.getCreateTime());
			System.out.println("当前节点的活动名称：" + task.getActivityName());
			System.out.println("执行对象ID：" + task.getExecutionId());
			System.out.println("-----------------------------");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cysyz.mylib.jbpm.impl.IJBPMCommonService#getPic(java.lang.String)
	 */
	public ImageInfo getPic(String key) {
		ProcessDefinition processDefinition = this
				.findLastVersionProcessDefinitionByKey(key);
		ImageInfo imageInfo = new ImageInfo();
		imageInfo.setImageName(processDefinition.getImageResourceName());
		imageInfo.setInputStream(simpleOperator.findImage(
				processDefinition.getDeploymentId(),
				processDefinition.getImageResourceName()));
		return imageInfo;
	}

	public static class ImageInfo {

		private String imageName;
		private InputStream inputStream;

		public String getImageName() {
			return imageName;
		}

		public void setImageName(String imageName) {
			this.imageName = imageName;
		}

		public InputStream getInputStream() {
			return inputStream;
		}

		public void setInputStream(InputStream inputStream) {
			this.inputStream = inputStream;
		}
	}
}
